$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'StudentAttendenceInfo/Select',
        datatype: "json",
        colModel: [
            { label: 'ID', name: 'id', width: 40 ,hidden:true,key:true},
            { label: '日期', name: 'date', width: 40 },
            { label: '学院', name: 'department', width: 40 },
             { label: '迟到人数', name: 'chidao', width: 40 },
             { label: '旷课人数', name: 'kuangke', width: 40 },
             { label: '早退人数', name: 'zaotui', width: 40 },
             { label: '请假人数', name: 'qingjia', width: 40 },
             { label: '出勤人数', name: 'chuqin', width: 40 }
         ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "scroll" });
        },
        ondblClickRow:function(rowid){
            var rowData = $("#jqGrid").jqGrid('getRowData',rowid);
            var rowName =$("#jqGrid").jqGrid('getGridParam','colNames');
            var s="";
            var i=2;
            for(var key in rowData){
                s=s+rowName[i++]+':'+rowData[key]+'<br> ';
            }
            alert(s);
        }
    });
});

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            key: null
        },
        showList: true,
        title: null,
        StudentAttendenceInfo: {},
        date: null
    },
    methods: {
        query: function () {
            $("#exportExcel").attr('href',baseURL+'StudentAttendenceInfo/export?token='+token+"&department="+vm.q.key);
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.StudentAttendenceInfo = {};
        },
        update: function () {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            $.get(baseURL + "StudentAttendenceInfo/Info/"+id, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.StudentAttendenceInfo = r.StudentAttendenceInfo;
                vm.date = r.StudentAttendenceInfo.date;
            });
        },
        del: function () {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "StudentAttendenceInfo/Delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {
            if(vm.validator()){
                return ;
            }

            var url = vm.StudentAttendenceInfo.date == vm.date ? "StudentAttendenceInfo/Update" : "StudentAttendenceInfo/Insert";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.StudentAttendenceInfo),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{ 'department': vm.q.key},
                page:page
            }).trigger("reloadGrid");
        },
        validator: function () {
            if(isBlank(vm.StudentAttendenceInfo.date)){
                alert("日期不能为空");
                return true;
            }
            if(isBlank(vm.StudentAttendenceInfo.chidao)){
                alert("迟到人数不能为空");
                return true;
            }
            if(isBlank(vm.StudentAttendenceInfo.kuangke)){
                alert("旷课人数不能为空");
                return true;
            }
            if(isBlank(vm.StudentAttendenceInfo.zaotui)){
                alert("早退人数不能为空");
                return true;
            }
            if(isBlank(vm.StudentAttendenceInfo.qingjia)){
                alert("请假人数不能为空");
                return true;
            }
            if(isBlank(vm.StudentAttendenceInfo.chuqin)){
                alert("出勤人数不能为空");
                return true;
            }
            }
        }
});